import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { SearchOutlined, EyeOutlined, QuestionCircleOutlined } from '@ant-design/icons';
import { TagSelectNew } from '@/components/TagSelectNew';
import { UploadFormCommon } from '@/components/UploadForm';
import {
  getLoadMapList,
  getTenantID,
  getUserDepName,
  getFileNameByPath,
  getUserDepID,
  renderTableTextNotColor,
  getSelectDataMapList,
  getCompanyID,
  getPosIDs,
  getUserID,
  getUserName,
  getUserShortCode,
  getColorTextLoadMap,
  splitFileUrl,
  downloadTemplate,
  getFileNameNotType,
} from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { useDescriptionsType } from '@/components/DescriptionsType';
import { RegularExpression } from '@/common/constVariable';
import moment from 'moment';
import { DatePicker, Form, Input, InputNumber, Select, Slider, Tooltip, TreeSelect, message } from 'antd';
import { MyAssetListManageAPI } from '../common/type';
import MyIcon from '@/components/MyIcon';

const getUpdateConfig = ({
  pageType,
  record,
  bothForm,
  changeAssetType,
  assetPurchaseType,
  assetAcquireType,
}: MyAssetListManageAPI.AcceptLocationPropsStateType) => {
  /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`, //资产类型
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_BRAND}`, //材料品牌
    `${getTenantID()}${ILoadMapTypeAll.AVERAGE_LIFE_METHOD}`, //折旧方法
    `${getTenantID()}${ILoadMapTypeAll.MATERIEL_STATUS}`, //材料状态
    `${getTenantID()}${ILoadMapTypeAll.ASSET_SOURCE_TYPE}`, //材料状态
  ]);
  /** 资产类型 */
  const assetType = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`], 'name', 'id', true);
  /** 材料品牌 */
  const assetBrand = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.MATERIAL_BRAND}`], 'name', 'id', true);
  /** 折旧方法 */
  const averageLifeMethod = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.AVERAGE_LIFE_METHOD}`], 'name', 'id', true);
  /** 材料状态 */
  const assetStatusList = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.MATERIEL_STATUS}`], 'name', 'id', true);
  /** 来源类型 */
  const assetSourcesType = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_SOURCE_TYPE}`], 'name', 'id', true);

  useEffect(() => {
    /** 回填表单 */
    if (pageType === 'detail' || pageType === 'edit') {
      bothForm?.setFieldsValue({
        ...record,
      });
    }
  }, [record]);
  /** 表单配置 */
  const [bothConfig, bothMethods] = useDescriptionsType({
    labelWidth: 190,
    basicForm: bothForm,
    name: 'accept-bothForm',
    schemas: [
      {
        key: 'accept-header',
        title: '',
        fieldChildren: [
          {
            field: `ledgerAccount`,
            label: `单位会计科目`,
            component: 'Render',
            disabled: true,
            required: true,
            placeholder: '请选择',
            render: (text) => {
              return (
                <Select
                  loading={mapLoading}
                  options={[]}
                  disabled={pageType === 'detail'}
                  placeholder="请选择"
                  fieldNames={{
                    label: 'name',
                    value: 'id',
                  }}
                />
              );
            },
          },
          {
            field: `assetType`,
            label: `资产分类`,
            component: 'Render',
            placeholder: '请选择',
            disabled: pageType == 'detail' || pageType == 'edit',
            required: true,
            render: (text) => {
              return (
                <TreeSelect
                  loading={mapLoading}
                  disabled={pageType == 'detail' || pageType == 'edit'}
                  showArrow={pageType === 'detail'}
                  placeholder="请选择"
                  treeData={loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`]}
                  showSearch={true}
                  fieldNames={{
                    label: 'name',
                    value: 'id',
                    children: 'children',
                  }}
                  treeNodeFilterProp={'name'}
                  onSelect={(_value, node) => {
                    // changeFormType(_value);
                    changeAssetType(_value);
                    bothForm?.setFieldsValue({
                      materialType: _value,
                    });
                  }}
                />
              );
            },
          },
        ],
      },
      {
        key: 'accept-basic',
        title: '基本信息',
        fieldChildren: [
          {
            field: `assetCode`,
            label: `资产编号`,
            component: 'Input',
            disabled: pageType === 'detail',
            placeholder: '请输入',
            required: true,
          },
          {
            field: `assetName`,
            label: `资产名称`,
            component: 'Input',
            disabled: pageType === 'detail',
            placeholder: '请选择',
            required: true,
          },
          {
            field: `quantityOrArea`,
            label: `数量/面积`,
            required: true,
            component: 'InputNumber',
            disabled: pageType === 'detail',
            placeholder: '请输入',
            min: 0,
            pattern: RegularExpression?.twoFloatNumber,
            patternMsg: '请输入整数或保留两位小数',
          },
          {
            field: `jno`,
            label: `项目代码`,
            disabled: pageType === 'detail',
            component: 'Input',
            placeholder: '请输入',
            required: true,
          },
          {
            field: `unitOfMeasurement`,
            label: `计量单位`,
            disabled: pageType === 'detail',
            component: 'Render',
            required: true,
            render: (text) => {
              return (
                <Select
                  loading={mapLoading}
                  options={loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.MATERIAL_MIDI}`]}
                  disabled={pageType === 'detail'}
                  placeholder="请选择"
                  fieldNames={{
                    label: 'name',
                    value: 'id',
                  }}
                />
              );
            },
          },
          {
            field: `procureOrgType`,
            label: `采购组织形式`,
            disabled: pageType === 'detail',
            component: 'Render',
            required: true,
            render: (text) => {
              if (pageType === 'detail') {
                return <span style={{ paddingLeft: 8 }}>{text ? text : '--'}</span>;
              } else {
                return (
                  <Select
                    loading={mapLoading}
                    options={assetPurchaseType}
                    disabled={pageType === 'detail'}
                    placeholder="请选择"
                    fieldNames={{
                      label: 'name',
                      value: 'id',
                    }}
                  />
                );
              }
            },
          },
          {
            field: `obtainType`,
            label: `取得方式`,
            disabled: pageType === 'detail',
            component: 'Render',
            required: true,
            render: (text) => {
              return (
                <Select
                  loading={mapLoading}
                  options={assetAcquireType}
                  disabled={pageType === 'detail'}
                  placeholder="请选择"
                  fieldNames={{
                    label: 'name',
                    value: 'id',
                  }}
                />
              );
            },
          },
          {
            field: `obtainDate`,
            label: `取得日期`,
            component: 'DatePicker',
            disabled: pageType === 'detail',
            format: 'YYYY-MM-DD',
            placeholder: '请选择',
            required: true,
          },
          {
            field: `usedType`,
            label: `资产用途`,
            disabled: pageType === 'detail',
            component: 'Input',
            placeholder: '请输入',
            required: true,
          },
          {
            field: `operationalDate`,
            label: `投入使用日期`,
            component: 'Render',
            disabled: pageType === 'detail',
            format: 'YYYY-MM-DD',
            placeholder: '请选择',
            render: (text) => {
              if (pageType === 'detail') {
                return <span style={{ paddingLeft: 8 }}>{text ? text : '--'}</span>;
              } else {
                return (
                  <Form.Item name="operationalDate">
                    <DatePicker placeholder="请选择" format="YYYY-MM-DD" />
                  </Form.Item>
                );
              }
            },
          },
          {
            field: `approvedBy`,
            label: `配置批准单位`,
            disabled: pageType === 'detail',
            component: 'Input',
            placeholder: '请输入',
            required: true,
          },
          {
            field: `inventoryNumber`,
            label: `清查编号`,
            component: 'Render',
            disabled: pageType === 'detail',
            required: true,
            placeholder: '请选择',
            render: (text) => {
              return (
                <Select
                  loading={mapLoading}
                  options={[]}
                  disabled={pageType === 'detail'}
                  placeholder="请选择"
                  fieldNames={{
                    label: 'name',
                    value: 'id',
                  }}
                />
              );
            },
          },
        ],
      },
    ],
  });

  return {
    bothConfig,
    bothMethods,
  };
};

export { getUpdateConfig };
